# -*- mode: python; -*-
import os
import subprocess
import sys

vpath = '#/third_party/openvswitch-2.3.0'

env = DefaultEnvironment()
env['ENV']['CFLAGS'] = '-DOPEN_CONTRAIL_CLIENT -g'

config_opts = '--prefix=' + str(Dir('#/build'))

if not sys.platform.startswith('freebsd'):
    cmd = ('(cd ' + Dir('.').abspath + '; ' + str(Dir(vpath)) +
           '/configure' + ' ' + config_opts + '; make clean; make; make install)')
else:
    cmd = ('(cd ' + Dir('.').abspath + '; ' + str(Dir(vpath)) +
           '/configure' + ' ' + config_opts + '; gmake clean; gmake; gmake install)')

libs = [str(File('#build/lib/libopenvswitch.a'))]

openvswitch = env.Command('config.status', str(Dir(vpath)), cmd)

env.SideEffect(libs, openvswitch)

def Symlink(target, source):
    parent = os.path.dirname(target[0].abspath)
    if not os.path.exists(parent):
        os.makedirs(parent)

    if not os.path.exists(target[0].abspath):
        os.symlink(source[0].abspath, target[0].abspath)

Symlink([Dir('#/build/include/openvswitch/include')], [Dir(vpath + '/include')])
Symlink([Dir('#/build/include/openvswitch/lib')], [Dir(vpath + '/lib')])
